google.setOnLoadCallback(function() {
	var options = { 
        target:        '#afterSubmit',   // target element(s) to be updated with server response 
        beforeSubmit:  validateB4Submit,  // pre-submit callback 
        success:       processAfterResponding  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    };	
	$('#user_signin').ajaxForm(options); 
	
	var option2 = { 
      //  target:        '#afterSubmit',   
        beforeSubmit:  validateB4SubmitLogin,  
        success:       processAfterRespondingLogin 
    };	
	$('#user_login').ajaxForm(option2); 
	
	var option3 = { 
      //  target:        '#afterSubmit',   
        beforeSubmit:  validateB4SubmitForgotPasswd,  
        success:       processAfterRespondingForgotPasswd
    };	
	$('#submit_email_forgot').ajaxForm(option3); 
});

function validateB4SubmitForgotPasswd(formData, jqForm, options){
	$('#loader').show();
	if( !isEmail($('#email').val())){
		$('#email').addClass('error');
		$('#loader').hide();
		return false;
	}
	else{
		$('#loader').show();
		$('#email').removeClass('error');
	}
	
	if(  ( md5($('#captcha').val()) != $('#captcha_md5').val() ) ){
		$('#captcha_error').show();
		$('#loader').hide();
		return false;
	}else{
		$('#captcha_error').hide();
		$('#loader').show();
	}
	return true;
}

function processAfterRespondingForgotPasswd(responseText, statusText, xhr, $form) {
	 
	if(responseText == 'nok'){
		$('#loader').hide();
		$('#email_error').show();
	}else{
		window.location = BASE_URI+'user/forgot_password_notity';
	}
}

function validateB4SubmitLogin(formData, jqForm, options){
	$('#loader').show();
//	if( !isEmail($('#email').val()) ){
//		$('#email').addClass('error');
//		$('#loader').hide();
//		return false;
//	}else{
		$('#loader').show();
		$('#email').removeClass('error');
//	}
	return true;
}

function processAfterRespondingLogin(responseText, statusText, xhr, $form) {
	if(responseText == 'nok'){
		$('#loader').hide();
		$('#email_error').show();
	}else{
		window.location = responseText;
	}
}

function validateB4Submit(formData, jqForm, options){
	$('#loader').show();
	if($('#buyer_type').val() == 'corporate_brand'){
		$checkURL = true;
	}else{
		$checkURL = false;
	}
	json = {};
	for( e in formData){
		json[formData[e].name] = formData[e].value;
	}
	if($checkURL){
		var arrayRequire = ['type_acc','email','first_name','last_name','job_title','company_url','company','country','address_line_1','city','state','zip','work_phone']; 
	}else{
		var arrayRequire = ['type_acc','email','first_name','last_name','job_title','company','country','address_line_1','city','state','zip','work_phone']; 
	}
	var err = 0;
	for(var i=0 ;i< arrayRequire.length; i++ ){
		if( isEmpty(json[arrayRequire[i]]) ){
			$('#'+arrayRequire[i]+'_error').show();
			err++;
		}else{
			$('#'+arrayRequire[i]+'_error').hide();
		}
	}
	if(! isEmail(json.email)){
		$('#email_error').show();
		err++;
	}else{
		$('#email_error').hide();
	}
	if($checkURL){
		if(! isUrl(json.company_url)){
			$('#company_url_error').show();
			err++;
		}else{
			$('#company_url_error').hide();
		}
	}
	if(err > 0){	
		$('#loader').hide();
		self.scrollTo(200,200);
		return false;
	}else{
		if(isEmpty($('#captcha').val())){
			$('#captcha_error').show();
			return false;
		}else{
			if( md5($('#captcha').val()) === $('#captcha_md5').val() ){
				$('#captcha_error').hide();
				return true;
			}else{
				$('#loader').hide();
				$('#captcha_error').show();
				return false;
			}
		}	  
	}
}


function processAfterResponding(responseText, statusText, xhr, $form) {
	$('#loader').hide();	
	
	if(responseText == 'ok'){
		$('#afterSubmit').hide();
		window.location = BASE_URI+'user/signin_notify';
	}else{
		var arr_res = responseText.split('#');
		for(i in arr_res){
			$('#'+arr_res[i]+'_error').removeClass('hidden');
		}
	}
}

function checkall_music(checked){
	if(checked){
		$('input[name="music_genre[]"]').attr('checked',true);
	}else{
		$('input[name="music_genre[]"]').attr('checked',false);
	}
}